package com.monkeyk.sos.domain.dto;

import java.io.Serializable;

import org.apache.commons.lang.StringUtils;

import com.monkeyk.sos.domain.model.ApplicationDetails;
import com.monkeyk.sos.domain.oauth.OauthClientDetails;
import com.monkeyk.sos.infrastructure.DateUtil;

/**
 * 应用信息封装
 */
public class ApplicationDetailsDto implements Serializable {

	private static final long serialVersionUID = -5221593156995890104L;
	private String appId;
	private String appName;
	private String appPlatform;
	private String appProfile;
	private String appDescribe;
	private String webName;
	private String webDomains;
	private String webRecord;
	private int status;
	
	private String	createTime;
	private boolean	archived;

	private String	clientId;
	private String	resourceIds;

	private String	clientSecret;

	private String	scope;

	private String	authorizedGrantTypes;

	private String	webServerRedirectUri;

	private String	authorities;

	private Integer	accessTokenValidity;

	private Integer	refreshTokenValidity;

	// optional
	private String	additionalInformation;

	private boolean	trusted;

	public ApplicationDetailsDto() {
	}
	public ApplicationDetailsDto(OauthClientDetails clientDetails,ApplicationDetails applicationDetails) {
		this.appId = applicationDetails.getAppId().toString();
		this.appName = applicationDetails.getAppName();
		this.appPlatform = applicationDetails.getAppPlatform();
		this.appProfile = applicationDetails.getAppProfile();
		this.appDescribe = applicationDetails.getAppDescribe();
		this.webName = applicationDetails.getWebName();
		this.webDomains = applicationDetails.getWebDomains();
		this.webRecord = applicationDetails.getWebRecord();
		this.status=applicationDetails.getStatus();
		this.clientId = clientDetails.clientId();
		this.clientSecret = clientDetails.clientSecret();
		this.scope = clientDetails.scope();
		this.createTime = DateUtil.getTodayHHandMM(clientDetails.createTime());
		this.archived = clientDetails.archived();
		this.resourceIds = clientDetails.resourceIds();
		this.webServerRedirectUri = clientDetails.webServerRedirectUri();
		this.authorities = clientDetails.authorities();
		this.accessTokenValidity = clientDetails.accessTokenValidity();
		this.refreshTokenValidity = clientDetails.refreshTokenValidity();
		this.additionalInformation = clientDetails.additionalInformation();
		this.trusted = clientDetails.trusted();
		this.authorizedGrantTypes = clientDetails.authorizedGrantTypes();
	}

	public String getAppId() {
		return appId;
	}
	public void setAppId(String appId) {
		this.appId = appId;
	}
	public String getAppName() {
		return appName;
	}

	public void setAppName(String appName) {
		this.appName = appName;
	}

	public String getAppPlatform() {
		return appPlatform;
	}

	public void setAppPlatform(String appPlatform) {
		this.appPlatform = appPlatform;
	}

	public String getAppProfile() {
		return appProfile;
	}

	public void setAppProfile(String appProfile) {
		this.appProfile = appProfile;
	}

	public String getAppDescribe() {
		return appDescribe;
	}

	public void setAppDescribe(String appDescribe) {
		this.appDescribe = appDescribe;
	}

	public String getWebName() {
		return webName;
	}

	public void setWebName(String webName) {
		this.webName = webName;
	}

	public String getWebDomains() {
		return webDomains;
	}

	public void setWebDomains(String webDomains) {
		this.webDomains = webDomains;
	}


	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public boolean isArchived() {
		return archived;
	}

	public void setArchived(boolean archived) {
		this.archived = archived;
	}

	public String getClientId() {
		return clientId;
	}

	public void setClientId(String clientId) {
		this.clientId = clientId;
	}

	public String getResourceIds() {
		return resourceIds;
	}

	public void setResourceIds(String resourceIds) {
		this.resourceIds = resourceIds;
	}

	public String getClientSecret() {
		return clientSecret;
	}

	public void setClientSecret(String clientSecret) {
		this.clientSecret = clientSecret;
	}

	public String getScope() {
		return scope;
	}

	public String getScopeWithBlank() {
		if (scope != null && scope.contains(",")) {
			return scope.replaceAll(",", " ");
		}
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public String getAuthorizedGrantTypes() {
		return authorizedGrantTypes;
	}

	public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
		this.authorizedGrantTypes = authorizedGrantTypes;
	}

	public String getWebServerRedirectUri() {
		return webServerRedirectUri;
	}

	public void setWebServerRedirectUri(String webServerRedirectUri) {
		this.webServerRedirectUri = webServerRedirectUri;
	}

	public String getAuthorities() {
		return authorities;
	}

	public void setAuthorities(String authorities) {
		this.authorities = authorities;
	}

	public Integer getAccessTokenValidity() {
		return accessTokenValidity;
	}

	public void setAccessTokenValidity(Integer accessTokenValidity) {
		this.accessTokenValidity = accessTokenValidity;
	}

	public Integer getRefreshTokenValidity() {
		return refreshTokenValidity;
	}

	public void setRefreshTokenValidity(Integer refreshTokenValidity) {
		this.refreshTokenValidity = refreshTokenValidity;
	}

	public String getAdditionalInformation() {
		return additionalInformation;
	}

	public void setAdditionalInformation(String additionalInformation) {
		this.additionalInformation = additionalInformation;
	}

	public boolean isTrusted() {
		return trusted;
	}

	public void setTrusted(boolean trusted) {
		this.trusted = trusted;
	}


	public boolean isContainsAuthorizationCode() {
		return this.authorizedGrantTypes.contains("authorization_code");
	}

	public boolean isContainsPassword() {
		return this.authorizedGrantTypes.contains("password");
	}

	public boolean isContainsImplicit() {
		return this.authorizedGrantTypes.contains("implicit");
	}

	public boolean isContainsClientCredentials() {
		return this.authorizedGrantTypes.contains("client_credentials");
	}

	public boolean isContainsRefreshToken() {
		return this.authorizedGrantTypes.contains("refresh_token");
	}

	public String getWebRecord() {
		return webRecord;
	}
	public void setWebRecord(String webRecord) {
		this.webRecord = webRecord;
	}
	
	public int getStatus() {
		return status;
	}
	public void setStatus(int status) {
		this.status = status;
	}
	public OauthClientDetails createDomain() {
		OauthClientDetails clientDetails = new OauthClientDetails().clientId(clientId).clientSecret(clientSecret)
				.resourceIds(resourceIds).authorizedGrantTypes(authorizedGrantTypes).scope(scope);
		if (StringUtils.isNotEmpty(webServerRedirectUri)) {
			clientDetails.webServerRedirectUri(webServerRedirectUri);
		}
		if (StringUtils.isNotEmpty(authorities)) {
			clientDetails.authorities(authorities);
		}
		clientDetails.accessTokenValidity(accessTokenValidity).refreshTokenValidity(refreshTokenValidity)
				.trusted(trusted);
		if (StringUtils.isNotEmpty(additionalInformation)) {
			clientDetails.additionalInformation(additionalInformation);
		}
		return clientDetails;
	}
	
	public ApplicationDetails createApplicationDetails() {
		ApplicationDetails applicationDetails = new ApplicationDetails();
		applicationDetails.setAppName(appName);
		applicationDetails.setAppPlatform(appPlatform);
		applicationDetails.setAppProfile(appProfile);
		applicationDetails.setAppDescribe(appDescribe);
		applicationDetails.setWebName(webName);
		applicationDetails.setWebDomains(webDomains);
		applicationDetails.setWebRecord(webRecord);
		applicationDetails.setStatus(status);
		return applicationDetails;
	}
}
